package consumer.proxy;

import consumer.util.ClientUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName ServerProxy
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2022/1/3 下午10:22
 * @Version V1.0
 * @Since JDK 11
 */
@Aspect
@Component
public class ServerProxy {

    @Value("${server.url}")
    private String url;

    @Pointcut("execution(* consumer.service..*.*(..))")
    public void point(){
    }

    @Around(value = "point()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        Class<?> aClass = joinPoint.getThis().getClass();
        String typeName = aClass.getSimpleName();
        String className = typeName.substring(0, typeName.indexOf("$$"));
        Class[] classes = null;
        if(args != null && args.length > 0){
            classes = new Class[args.length];
            int cnt = 0;
            for (Object arg : args) {
                classes[cnt] = arg.getClass();
                cnt++;
            }
        }
        return ClientUtil.sendMsg(url,className,methodName,classes,args);
    }
}
